package com.healthcloud.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.healthcloud.service.HealthSurveyService;
import com.healthcloud.utils.ApiRes;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 健康体检问诊接口服务端控制器
 * @author ZhengL 
 * 2015-07-10
 */
@ApiIgnore
@Api(value = "示例说明", description = "健康体检问诊接口服务端控制器")
@RestController
@RequestMapping("/app/healthSurvey")
public class HealthSurveyApiController {

	String[] hcDietaryNutrition = { "op", "username", "usertype", "token",
			"timestamp", "v", "appid", "sign", "id", "idCard", "examId",
			"mkExamCode", "answer1", "answer2", "answer3", "answer4",
			"answer5", "answer6", "answer7", "answer8", "answer9", "answer10",
			"answer11", "answer12", "answer13", "answer14","answer15" };
	String[] hcHealthStatusAndOccupationalHistory = { "op", "username",
			"usertype", "token", "timestamp", "v", "appid", "sign", "id",
			"idCard", "examId", "mkExamCode", "answer1", "answer2", "answer3",
			"answer4", "answer5", "answer6","answer7"};
	String[] hcLifeStyleAndHabits = { "op", "username", "usertype", "token",
			"timestamp", "v", "appid", "sign", "id", "idCard", "examId",
			"mkExamCode", "answerA1", "answerA2", "answerA3", "answerA4",
			"answerB1", "answerB2", "answerC1", "answerC2", "answerC3" };
	String[] hcSportMode = { "op", "username", "usertype", "token",
			"timestamp", "v", "appid", "sign", "id", "idCard", "examId",
			"mkExamCode", "answer1", "answer2", "answer3", "answer4",
			"answer5", "answer6" };
	String[] comm = { "op", "username", "usertype", "token",
			"timestamp", "v", "appid", "sign", "id", "idCard", "examId",
			"mkExamCode"};

	@Autowired
	private HealthSurveyService healthSurveyService;
	
	/**
	 * 移动端上传健康调查接口
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "移动端上传健康调查接口",notes ="移动端上传健康调查接口")
	@RequestMapping(value = "/set", method = RequestMethod.POST)
	public Object saveSurvery(HttpServletRequest request) {
		String[] str = null;
		Map<String, String> dataMap = getDataMap(request,str);
		ApiRes jm = new ApiRes(healthSurveyService.parseJsonMap(dataMap));
		jm.setSuccess(true);
		return jm;
	}

	// 验证方法
	private ApiRes verify(Map<String, String> jsonMap, ApiRes msg) {
		String username = jsonMap.get("username");
		String usertype = jsonMap.get("usertype");
		String token = jsonMap.get("token");
		String timestamp = jsonMap.get("timestamp");
		String v = jsonMap.get("v");
		String appid = jsonMap.get("appid");
		String sign = jsonMap.get("sign");
		// if(StringUtil.isBlank(token)){
		// msg.setSuccess(false);
		// msg.setError("missing_token");
		// }else{
		// OauthSubjectWrapper wrapper = cache.get(token);
		// if (wrapper == null) {
		// msg.setSuccess(false);
		// msg.setError("invalid_token");
		// }
		// }
		msg.setSuccess(true);
		return msg;
	}

	// 获取请求中的参数
	private Map<String, String> getDataMap(HttpServletRequest request,
			String[] strs) {
		Map<String, String> dataMap = new HashMap<String, String>();
		Map<String, String[]> params = request.getParameterMap();
		if(params.containsKey("op")){
			String op = params.get("op")[0].toString();
			if (op == "hc_dietarynutrition" || op.equals("hc_dietarynutrition")) {
				strs = hcDietaryNutrition;
			} else if (op == "hc_healthstatusandoccupationalhistory"
					|| op.equals("hc_healthstatusandoccupationalhistory")) {
				strs = hcHealthStatusAndOccupationalHistory;
			} else if (op == "hc_lifestyleandhabits"
					|| op.equals("hc_lifestyleandhabits")) {
				strs = hcLifeStyleAndHabits;
			} else if(op=="hc_sportmode"||op.equals("hc_sportmode")){
				strs = hcSportMode;
			}else{
				strs = comm;
			}
		}
		for(int i=0;i<strs.length;i++){
			String key = strs[i].trim().toString();
			if (params.containsKey(key)) {
				String[] values = params.get(key);
				dataMap.put(key, values[0].toString());
			}
		}
		return dataMap;
	}
}
